package com.jf.mall.dao.impl;

import com.jf.mall.dao.BasicDAO;
import com.jf.mall.dao.UserDAO;
import com.jf.mall.domain.User;

/**
 * Created with IntelliJ IDEA
 * Description:
 * User:22954
 * Date:2024-09-23
 * Time:17:01
 */
public class UserDAOImpl extends BasicDAO<User> implements UserDAO {
    @Override
    public User queryUserByUsername(String username) {
        String sql = "select * from `user` where username = ? and `signal` = 0";
        return querySingle(sql, User.class, username);
    }

    @Override
    public User queryUserByUsernameAndPassword(String username, String password, boolean signal) {
        String sql = "select * from `user` where username = ? and password = md5(?) and `signal` = ?";
        int i = signal ? 1 : 0;
        return querySingle(sql, User.class, username, password, signal);
    }

    @Override
    public User queryUserByUsernameAndPassword(String username, String password) {
        return queryUserByUsernameAndPassword(username, password, false);
    }

    @Override
    public int saveUser(User user) {
        String sql = "insert into `user`(username, 'password', email) values(?, md5(?), ?)";
        String username = user.getUsername();
        String password = user.getPassword();
        String email = user.getEmail();
        try {
            return update(sql, username, password, email);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
